package com.junt.library.core;

import android.net.Uri;
import android.provider.MediaStore;

import com.junt.library.MediaSelector;
import com.junt.library.core.QueryType;

/**
 * 用于ContentResolver进行查询的配置参数
 */
public class QueryParam {

    private QueryType queryType;

    public QueryParam() {
        this.queryType = MediaSelector.getInstance().getQueryType();
    }

    /**
     * 查询column
     * eg:
     * new String[] {
     * MediaStore.Video.Media._ID,
     * MediaStore.Video.Media.DISPLAY_NAME,
     * MediaStore.Video.Media.DURATION,
     * MediaStore.Video.Media.SIZE
     * };
     */
    public String[] getProjection() {
        switch (queryType) {
            case VIDEO:
                return new String[]{
                        MediaStore.MediaColumns._ID,
                        MediaStore.MediaColumns.DISPLAY_NAME,
                        MediaStore.MediaColumns.SIZE,
                        MediaStore.MediaColumns.DATE_MODIFIED,
                        MediaStore.MediaColumns.DATA,
                        MediaStore.MediaColumns.MIME_TYPE,
                        MediaStore.Video.VideoColumns.WIDTH,
                        MediaStore.Video.VideoColumns.HEIGHT,
                        "orientation",
                        "duration",
                        "relative_path"};
            case AUDIO:
                return new String[]{
                        MediaStore.MediaColumns._ID,
                        MediaStore.MediaColumns.DISPLAY_NAME,
                        MediaStore.MediaColumns.SIZE,
                        MediaStore.MediaColumns.DATE_MODIFIED,
                        MediaStore.MediaColumns.DATA,
                        MediaStore.MediaColumns.MIME_TYPE,
                        "duration",
                        "relative_path"};
            case IMAGE:
            default:
                return new String[]{
                        MediaStore.MediaColumns._ID,
                        MediaStore.MediaColumns.DISPLAY_NAME,
                        MediaStore.MediaColumns.SIZE,
                        MediaStore.MediaColumns.DATE_MODIFIED,
                        MediaStore.MediaColumns.DATA,
                        MediaStore.MediaColumns.MIME_TYPE,
                        MediaStore.Images.ImageColumns.WIDTH,
                        MediaStore.Images.ImageColumns.HEIGHT,
                        "orientation",
                        "relative_path"};
        }
    }

    /**
     * 筛选条件
     * eg:
     * String selection = MediaStore.Video.Media.SIZE + " >= ?";
     */
    public String getSelection() {
        long size = MediaSelector.getInstance().getMinSize();
        String fileSuffix=MediaSelector.getInstance().getMediaFormats();
        switch (queryType) {
            case VIDEO:
            case IMAGE:
            case AUDIO:
            default:
                return MediaStore.MediaColumns.SIZE + " >= "+size
                        + ") AND (" +
                        MediaStore.MediaColumns.DISPLAY_NAME + " like '%" + fileSuffix+"'";
        }
    }

    /**
     * 按修改时间排序
     * eg:
     *
     * @param asc true - 升序  false - 降序
     * @return "MediaStore.Video.Media.DISPLAY_NAME"+ " ASC"
     */
    public String getSortOrder(boolean asc) {
        String projection = null;
        switch (queryType) {
            case VIDEO:
            case IMAGE:
            case AUDIO:
                projection = MediaStore.Video.Media.DATE_MODIFIED;
                break;
            default:
                break;
        }
        return asc ? projection + " ASC" : projection + " DESC";
    }

    /**
     * Uri used to query
     */
    public Uri getContentUri() {
        return Uri.parse("content://media")
                .buildUpon()
                .appendPath("external")
                .appendPath(getPath())
                .appendPath("media")
                .build();
    }

    public String getPath() {
        switch (queryType) {
            case VIDEO:
                return "video";
            case IMAGE:
                return "images";
            case AUDIO:
                return "audio";
            default:
                return "";
        }
    }
}
